Circle Images
This script uses ImageMagick, a powerful command-line image processing tool. You must have it installed for this script to work.
The Bash Script
You can save this code as circularize.sh.
#!/bin/bash
# A script to convert an image into a circular one with a transparent background.
#
# DEPENDENCY: ImageMagick (must be installed)
# USAGE: ./circularize.sh /path/to/your/image.jpg
# --- Configuration ---
# Use 'magick' (ImageMagick v7+) or 'convert' (v6 or legacy)
# By default, we check for 'magick' first.
if command -v magick &> /dev/null; then
IM_COMMAND="magick"
elif command -v convert &> /dev/null; then
IM_COMMAND="convert"
else
echo "Error: ImageMagick is not installed." >&2
echo "Please install ImageMagick (https://imagemagick.org) to use this script." >&2
exit 1
fi
# --- Input Validation ---
if [ -z "$1" ]; then
echo "Usage: $0 <input_image>" >&2
exit 1
fi
INPUT_FILE="$1"
if [ ! -f "$INPUT_FILE" ]; then
echo "Error: File not found: $INPUT_FILE" >&2
exit 1
fi
# --- File Naming ---
# Get the input file's directory and base name
DIRNAME=$(dirname -- "$INPUT_FILE")
INPUT_BASENAME=$(basename -- "$INPUT_FILE")
# Remove the old extension (e.g., .jpg, .png)
FILENAME_NO_EXT="${INPUT_BASENAME%.*}"
# Define the output file path
OUTPUT_FILE="${DIRNAME}/${FILENAME_NO_EXT}_circular.png"
echo "Processing '$INPUT_FILE'..."
# --- Get Image Dimensions ---
# Get dimensions as a single "widthxheight" string
DIMS=$($IM_COMMAND identify -format "%wx%h" "$INPUT_FILE")
# Split into width and height
WIDTH=$(echo $DIMS | cut -dx -f1)
HEIGHT=$(echo $DIMS | cut -dx -f2)
# Determine the smallest dimension to create a square crop
if [ $WIDTH -lt $HEIGHT ]; then
SIZE=$WIDTH
else
SIZE=$HEIGHT
fi
# Calculate the center coordinate (half the size)
# This is used for drawing the circle
C=$(($SIZE / 2))
# --- ImageMagick Command ---
# This is the core logic
$IM_COMMAND "$INPUT_FILE" \
-alpha set \
-gravity center \
-crop ${SIZE}x${SIZE}+0+0 +repage \
\( \
+clone \
-channel A -evaluate set 0 \
-fill white \
-draw "circle $C,$C $C,0" \
\) \
-compose DstIn \
-composite \
"$OUTPUT_FILE"
# --- Final Check ---
if [ $? -eq 0 ]; then
echo "✅ Success! Circular image saved to: $OUTPUT_FILE"
else
echo "❌ Error: ImageMagick command failed." >&2
exit 1
fi
How to Use It
- Install ImageMagick:
- Fedora/RHEL: sudo dnf install ImageMagick
- Save the Script:
Copy the code above and save it to a file named circularize.sh.
- Make it Executable:
In your terminal, navigate to where you saved the file and run:
- Run the Script:
Pass the path to your image as an argument:
./circularize.sh my_photo.jpg
Or:
./circularize.sh /home/user/pictures/avatar.png
If you provided my_photo.jpg, the script will create a new file named my_photo_circular.png in the same directory.
How the ImageMagick Command Works
Here's a breakdown of the main command:
- $IM_COMMAND "$INPUT_FILE": Loads your input image.
- -alpha set: Ensures the image has a transparency (alpha) channel, even if it's a JPG.
- -gravity center -crop ${SIZE}x${SIZE}+0+0 +repage:
- -gravity center: Sets the "anchor" point for the crop to the center.
- -crop ${SIZE}x${SIZE}+0+0: Crops the image into a square based on the smallest dimension ($SIZE).
- +repage: Resets the image's "page" geometry, which is necessary after cropping.
- \( ... \): This starts an in-memory "sub-process". We are building a mask inside these parentheses.
- +clone: Makes a copy of our (now square) image. This copy will become the mask.
- -channel A -evaluate set 0: Selects the alpha channel (A) of the clone and sets it to 0 (fully transparent).
- -fill white -draw "circle $C,$C $C,0": Draws a solid white circle onto the transparent clone. The circle's center is at $C,$C and one edge is at $C,0 (the top-middle edge).
- \): The sub-process is done. We now have two images in memory: the original cropped photo and our white-circle-on-transparent-background mask.
- -compose DstIn -composite: This is the magic.
- -compose DstIn: Sets the composition method to "Destination In".
- -composite: Applies the composition. It keeps the parts of the Destination (our photo) that are In the Source (our white circle mask).
- "$OUTPUT_FILE": Saves the final result to the output PNG file.